package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    //查询
    @Override
    public List<Customer> getCustomerList(Integer page, Integer rows,String  customerName) {
        List<Customer> list=customerDao.selectCustomerList(page,rows,customerName);
        return list;
    }
    //添加或修改
    @Override
    public ServiceVO saveOrUpdate(Customer customer) {
        Integer customerId = customer.getCustomerId();
        if (customerId==null){
            String customerName = customer.getCustomerName();
            Customer customer1=customerDao.selectByName(customerName);
            if (customer1!=null){//这个用户名已经有了
                return new ServiceVO(ErrorCode.ACCOUNT_EXIST_CODE,ErrorCode.ACCOUNT_EXIST_MESS);//用户名已存在
            }
            customerDao.save(customer);
        }else {//修改
            String customerName = customer.getCustomerName();
            Customer customer1=customerDao.selectByName(customerName);
            if (customer1!=null){//这个用户名已经有了
                return new ServiceVO(ErrorCode.ACCOUNT_EXIST_CODE,ErrorCode.ACCOUNT_EXIST_MESS);//用户名已存在
            }
            customerDao.update(customer);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        customerDao.delete(list);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<Customer> getComboboxList(String customerName) {
        return customerDao.getComboboxList(customerName);
    }

}
